package com.hm.manage.service.impl;

import java.util.List;
import java.util.Vector;

import com.hm.manage.utils.SSHRemoteCall;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hm.manage.mapper.HardserverMapper;
import com.hm.manage.domain.Hardserver;
import com.hm.manage.service.IHardserverService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 服务器Service业务层处理
 * 
 * @author hm
 * @date 2025-04-01
 */
@Slf4j
@Service
public class HardserverServiceImpl extends ServiceImpl<HardserverMapper,Hardserver> implements IHardserverService
{
    @Autowired
    private HardserverMapper hardserverMapper;

    /**
     * 查询服务器
     * 
     * @param id 服务器主键
     * @return 服务器
     */
    @Override
    public Hardserver selectHardserverById(Long id)
    {
        return hardserverMapper.selectHardserverById(id);
    }

    /**
     * 查询服务器列表
     * 
     * @param hardserver 服务器
     * @return 服务器
     */
    @Override
    public List<Hardserver> selectHardserverList(Hardserver hardserver)
    {
        return hardserverMapper.selectHardserverList(hardserver);
    }

    /**
     * 新增服务器
     * 
     * @param hardserver 服务器
     * @return 结果
     */
    @Override
    public int insertHardserver(Hardserver hardserver)
    {
        return hardserverMapper.insertHardserver(hardserver);
    }

    /**
     * 修改服务器
     * 
     * @param hardserver 服务器
     * @return 结果
     */
    @Override
    public int updateHardserver(Hardserver hardserver)
    {
        return hardserverMapper.updateHardserver(hardserver);
    }

    /**
     * 批量删除服务器
     * 
     * @param ids 需要删除的服务器主键
     * @return 结果
     */
    @Override
    public int deleteHardserverByIds(Long[] ids)
    {
        return hardserverMapper.deleteHardserverByIds(ids);
    }

    /**
     * 删除服务器信息
     * 
     * @param id 服务器主键
     * @return 结果
     */
    @Override
    public int deleteHardserverById(Long id)
    {
        return hardserverMapper.deleteHardserverById(id);
    }

    @Override
    public String getServerInfo(Long id) {
        Hardserver hardserver = hardserverMapper.selectHardserverById(id);
        log.info("--------------------hardserver = {}",hardserver);
        String result = "无结果";
        if(hardserver!=null){
            try {
                // 1、远程ssh
                SSHRemoteCall
                        .getInstance2()
                        .sshRemoteCallLogin(hardserver.getServerIp(), Integer.parseInt(hardserver.getServerPort()), hardserver.getUsername(), hardserver.getUPassword());
                // 2、 执行命令
                String command = "du -sh";
                // 3、执行命令
                result = SSHRemoteCall.getInstance2().execCommand(command+" "+hardserver.getFilePath());
                log.info("---------execCommand2 = {}",result);
                //SSHRemoteCall.getInstance().closeSession();
                SSHRemoteCall.getInstance2().closelogin();
            } catch (Exception e) {
                // 打印报错
                log.info("---------连接失败-----------sshRemoteCallLogin = {}",e.getMessage());
                throw new RuntimeException(e);
            } finally {
                //SSHRemoteCall.getInstance2().closeSession();
            }
        }
        return result;
    }
}
